import translations from './translations';

export default function translate(template, replacements) {
  if (template.indexOf("Create") === 0 && typeof replacements == "undefined") {
    template = translate("Create {type}", {"type": template.split(" ", 2)[1]});
  } else if (template.indexOf("Append") === 0 && typeof replacements == "undefined") {
    template = translate("Append {type}", {"type": template.split(" ", 2)[1]});
  }
  
  replacements = replacements || {};

  // Translate
  template = translations[template] || template;
  
  // Replace
  return template.replace(/{([^}]+)}/g, function(_, key) {
    let str = replacements[key];
    if(translations[replacements[key]] != null && translations[replacements[key]] !== 'undefined'){
      str = translations[replacements[key]];
    }
    return  str || '{' + key + '}';
  });
}